import car from '../../../config/api/car' // 引入api接口管理文件
import userapi from '../../../config/api/userapi' // 引入api接口管理文件
const dayjs = require("./dayjs");
const app = getApp()
var WxNotificationCenter = require("../../../config/WxNotificationCenter");
import {
  baseUrl
} from '../../../config/config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: {},
    ferenceitem: {},
    name: null, //主题
    department: null, //部门
    phoneNumber: null, //手机号
    useCarName: null, // 人员名单
    userType: 0, //麦克类型
    userTypeArr: ["公务", "出差", "其他"],
    route: null, // 路线
    fee: null, // 费用
    schedule: null, //调度

    currdriver: null, // 当前选择司机
    driverlist: [], //司机列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {


    let theitem = JSON.parse(options['item']);
    let datestr = dayjs(theitem.date).format("YY/MM/DD");
    this.setData({
      item: theitem,
      // ferenceitem: theitem.ferenceitem,
      datestr: datestr
    });
    //加载司机列表
    // this.loadDriver();

    var that = this
    WxNotificationCenter.addNotification('selectdrivers', that.selectdriver, that)

  },
  //选择司机回调
  selectdriver(res) {
    console.log(res, "selectdriver");
    this.setData({
      currdriver: res
    })
  },
  //加载司机列表
  loadDriver() {
    let vm = this;
    userapi.getDriver({
      "pageNo": 1,
      "pageSize": 9999,
      "startTime": this.data.item.date + " " + this.data.item.stime + ":00",
      "endTime": this.data.item.date + " " + this.data.item.etime + ":00"
    }, function (res) {
      let list = res.data.data;
      console.log(list);
      vm.setData({
        driverlist: list
      });
      let csj;
      //补充当前司机
      for (let index = 0; index < list.length; index++) {
        const element = list[index];
        if (element.id * 1 == vm.data.ferenceitem['carOwner'] * 1) {
          csj = element;
        }
      }
      if (csj) {
        vm.setData({
          currdriver: csj
        });
      } else {
        vm.setData({
          currdriver: null
        });
      }
    })
  },
  onChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
  },
  clicktijiao() {
    //必填字段判空
    let pkmap = {
      "name": "请输入主题",
      "department": "请输入部门",
      "phoneNumber": "请输入手机号码",
      "useCarName": "请输入人员姓名"
    }
    let allkey = Object.keys(pkmap);
    for (let index = 0; index < allkey.length; index++) {
      const element = allkey[index];
      console.log(this.data[element]);
      if (!this.data[element] || this.data[element].length < 1) {
        wx.showToast({
          title: pkmap[element],
          icon: "none",
        })
        return;
      }
    }


    wx.showLoading({
      title: '预约中...',
      mask: true,
    })
    let obj = {
      "startTime": this.data.item.date + " " + this.data.item.stime + ":00",
      // "carId": this.data.ferenceitem.id,
      "openId": wx.getStorageSync('openid'),
      "endTime": this.data.item.date + " " + this.data.item.etime + ":00",
      "type": 2,
    };
    // if (this.data.currdriver) {
    //   obj['driverId'] = this.data.currdriver.id;
    // }
    //外部字段
    let outkeys = ['name', 'department', 'phoneNumber', 'useCarName'];
    //内部字段
    let detailkeys = ['fee', 'route', 'schedule', 'userType'];

    for (let index = 0; index < outkeys.length; index++) {
      const key = outkeys[index];
      let data = this.data[key];
      if (data && data.length > 0) {
        obj[key] = data
      }
    }
    let details = {
      "departureTime": this.data.item.date + " " + this.data.item.stime + ":00",
      "returnTime": this.data.item.date + " " + this.data.item.etime + ":00",
    };

    for (let index = 0; index < detailkeys.length; index++) {
      const key = detailkeys[index];
      let data = this.data[key];
      if (data && data.length > 0) {
        details[key] = data
      }
    }
    obj["carDetails"] = details
    let vm = this;
    car.save(obj, function (res) {
      wx.hideLoading({
        success: (res) => {},
      })
      if (res.data.status == 200) {
        WxNotificationCenter.postNotificationName("getcarlist");
        vm.yuyuesuccess();
      } else if (res.data.status == 1) {
        wx.showToast({
          title: "预定时间冲突",
          icon: "none"
        })
      } else if (res.data.status == 2) {
        wx.showToast({
          title: "车辆不存在",
          icon: "none"
        })
      } else if (res.data.status == 3) {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },
  // 预约成功
  yuyuesuccess() {
    wx.reLaunch({
      url: '../addsuccess/addsuccess',
    })
  },
  //处理文本双向绑定问题
  onChange(e) {
    console.log(e.detail.value) //input 的 value
    console.log(e.currentTarget.dataset.prop) //data-prop 绑定的字符串，以此来确定改变的是哪个变量
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail.value
    })
  },
  //处理是否选项
  shifouChange(e) {
    console.log(e.detail.value) //input 的 value
    console.log(e.currentTarget.dataset.prop) //data-prop 绑定的字符串，以此来确定改变的是哪个变量
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail.value,
    })
  },
  // 处理开始时间
  changestime(e) {
    let stime = e.detail.value
    let item = this.data.item;
    item.stime = stime;
    this.setData({
      item: item
    })
  },
  // 处理结束时间
  changeetime(e) {
    let etime = e.detail.value
    let item = this.data.item;
    item.etime = etime;
    this.setData({
      item: item
    })
  },
  //点击司机
  clicksiji(e) {
    let times = {
      "stime": this.data.item.date + " " + this.data.item.stime + ":00",
      "etime": this.data.item.date + " " + this.data.item.etime + ":00",
    };
    wx.navigateTo({
      url: '/pages/driver/driver?timeobj=' + JSON.stringify(times),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  onUnload() {
    var that = this
    WxNotificationCenter.removeNotification('selectdrivers', that)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})